#include "wrap.h"

void sys_err(const char *str){
	perror(str);
	exit(1);
}


// 1.重写Socket函数
int Socket(int domain, int type, int protocol){
	int n = socket(domain, type, protocol);
	if(n == -1){
		sys_err("socket error!");
		return -1;
	}
	return n;
}

// 2.重写bind函数
int Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen){
	int n = bind(sockfd, addr, addrlen);
	if(n == -1){
		sys_err("bind error");
		return -1;
	}
	return n;
}
// 3.重写listen函数
int Listen(int sockfd, int backlog){
	int n = listen(sockfd, backlog);
	if(n == -1){
		sys_err("listen error");
		return -1;
	}
	return n;
}
// 4.重写accept函数
int Accept(int sockfd, struct sockaddr *addr,socklen_t *addrlen){
	int n = accept(sockfd, addr, addrlen);
	if(n == -1){
		sys_err("accept error");
		return -1;
	}
	return n;
}
// 5.重写write和read函数
int Write(int fd, const void *buf, size_t count){
	int n = write(fd, buf, count);
	if( n == -1){
		sys_err("write error");
		return -1;
	}
	return n;
}
int Read(int fd, void *buf, size_t count){
	int n = read(fd, buf,count);
	if( n == -1){
		sys_err("read error");
		return -1;
	}
	return n;
}

// 重写connect函数
int Connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen){
	int n = connect(sockfd, addr, addrlen);
	if(n == -1){
		sys_err("connect error");
		return -1;
	}
	return n;
}
